init('Log');
(function(){
	var StringUtils  = require('./StringUtils.js');
	
	var Level = {
		ERROR : 2
		,WARN  : 1
		,INFO  : 0
		,DEBUG : -1
		,TRACE : -2		
	};
	
	var loglevel = Level.INFO;
	var loggers  = [];
	loggers.push({
		log : function(value){
			console.log(value);
		},
		level : function(){
			return loglevel;
		}
	})
	var getLevelName = function(level){
		if( level == Level.TRACE ) return 'TRACE';
		if( level == Level.DEBUG ) return 'DEBUG';
		if( level == Level.INFO  ) return 'INFO';
		if( level == Level.WARN  ) return 'WARN';
		if( level == Level.ERROR ) return 'ERROR';
		return "";
	};
	
	var Log = function(name){
		this.name = name;
		this.loglevel = loglevel;
	}
	Log.prototype = {
		_log : function(level, arguments){
			var format  = StringUtils.format;
			var fit     = StringUtils.fit;
			
			arguments[0] = "["+getLevelName(level) + "] "+fit(this.name, 10) + " : " + arguments[0];
			var value = format.apply(this, arguments)
			for(var i=0; i < loggers.length; i++){
				if( loggers[i].level() <= level ){
					loggers[i].log(value);
				}
			}
		},	
		warn : function(){
			this._log(Level.WARN, arguments);
		},
		info : function(){
			this._log(Level.INFO, arguments);
		},
		error : function(){
			this._log(Level.ERROR, arguments);
		},
		debug : function(){
			this._log(Level.DEBUG, arguments);
		},
		trace : function(){
			this._log(Level.TRACE, arguments);
		},
		log : function(){
			this._log(Level.ERROR, arguments);
		}
	};
	
	var LogFactory = function(){
		
	}
	LogFactory.prototype = {
		create : function(name){
			return new Log(name);
		},
		addLogger : function(appender){
			loggers.push(appender);
		}
	}
	LogFactory.Level = Level;
	
	if(module){
		module.exports = new LogFactory();
		module.exports.Level = Level;
	}
	
})();